package com.naiterui.ehp.bs.recommend.controller;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.recommend.exception.ExceptionCodes;
import com.naiterui.ehp.bs.recommend.remote.IRecommendRemoter;
import com.naiterui.ehp.bs.recommend.service.IDrDiagnosisService;
import com.naiterui.ehp.bs.recommend.swagger.Tags;
import com.naiterui.ehp.bs.recommend.vo.param.CommonDiagnosisVO;
import com.naiterui.ehp.bs.recommend.vo.param.DiagnosisVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 诊断相关
 *
 * @author wangchuan
 * @Date 2017年7月22日
 * @since V2.9.0
 */
@Api(tags = SwaggerTag.DOCTOR + SwaggerTag.DIAGNOSIS)
@RequestMapping("recommend/diagnosis")
@Controller
public class DiagnosisController {
    private static final Logger LOGGER = LoggerFactory.getLogger(DiagnosisController.class);

    @Autowired
    private IDrDiagnosisService diagnosisService;
    @Autowired
    private IRecommendRemoter remoter;

    /**
     * 诊断库abc列表
     *
     * @param doctorId
     *
     * @return
     *
     * @throws Exception
     * @since V3.5.0
     */

    @RequestMapping("new/list")
    @ApiOperation( value = "获取临床诊断列表", notes = "获取临床诊断ABC列表", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "long", paramType = "query") })
    @ResponseBody
    public ResponseVO<CommonDiagnosisVO> getNewDiagnosisList(Long doctorId) throws Exception {
        LOGGER.info("------medicationRecom getDiagnosisList param; doctorId={}", doctorId);
        if (doctorId == null) {
            LOGGER.info("获取临床诊断列表，医生ID为空");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        CommonDiagnosisVO commonDiagnosisVO = this.diagnosisService.findCommonDiagnosis(doctorId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, commonDiagnosisVO);
    }

    /**
     * 临床诊断搜索结果
     *
     * @param doctorId
     * @param keyword
     *
     * @return
     *
     * @throws BusinessException
     * @Description
     * @author wangchuan
     * @Date 2017年7月28日
     * @since V2.9.0
     */

    @RequestMapping("search")
    @ApiOperation(tags = { Tags.VERSION290, Tags.APP }, value = "临床诊断搜索列表", notes = "临床诊断搜索列表", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键字", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "skuIds", value = "skuIds列表,逗号分隔", required = false, dataType = "String", paramType = "query") })
    @ResponseBody
    public ResponseVO<PageVO<DiagnosisVO>> searchDiagnosis(Long doctorId, String keyword, PageParamVO paramVO, String skuIds) throws BusinessException {
        LOGGER.info("------medicationRecom searchDiagnosis param; doctorId={}, keyword={}, skuIds={}", doctorId, keyword, skuIds);
        if (doctorId == null) {
            LOGGER.info("临床诊断搜索列表，医生ID为空");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        if (keyword != null) {
            keyword = keyword.replace("　", "");
        }
        PageVO<DiagnosisVO> page = this.remoter.searchDiagnosis(doctorId, keyword, skuIds, paramVO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, page);
    }

    /**
     * 标准诊断库导入
     *
     * @param sign
     *
     * @return
     */

    @RequestMapping("import")
    @ApiIgnore
    @ResponseBody
    public ResponseVO<Void> importStandard(String sign, @RequestParam("file") MultipartFile file) throws Exception {
        LOGGER.info("improt standard diagnosis ,file names {} ", file.getOriginalFilename());
        if (!"12345678qwertpou".equals(sign)) {
            return PublicService.returnResponseVO(ExceptionCodes.PARAM_ERROR);
        }
        this.diagnosisService.saveImportFile(file);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }
}
